public class Solution844 {
    public boolean backspaceCompare(String s, String t) {
        StringBuilder stringBuilder=new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c=='#'){
                if (stringBuilder.length()>0){
                    stringBuilder.deleteCharAt(stringBuilder.length()-1);
                }
            }else{
                stringBuilder.append(c);
            }
        }
        String a=stringBuilder.toString();
        stringBuilder=new StringBuilder();
        for (char c : t.toCharArray()) {
            if (c=='#'){
                if (stringBuilder.length()>0){
                    stringBuilder.deleteCharAt(stringBuilder.length()-1);
                }
            }else{
                stringBuilder.append(c);
            }
        }
        return a.equals(stringBuilder.toString());
    }

    public static void main(String[] args) {
        System.out.println(new Solution844().backspaceCompare("ab#c","ad#c"));
    }
}
